import { ElMessageBox } from 'element-plus';

import { $t } from '#/locales';
import { useAuthStore } from '#/store';

export function useLoginStatusError() {
  const authStore = useAuthStore();

  /**
   * 显示登录状态错误确认对话框
   * @param errorMessage 错误信息
   */
  async function showLoginStatusErrorDialog(errorMessage?: string) {
    try {
      await ElMessageBox.confirm(
        errorMessage || $t('authentication.loginStatusErrorDesc'),
        $t('authentication.loginStatusError'),
        {
          confirmButtonText: $t('common.confirm'),
          type: 'warning',
          showCancelButton: false,
          closeOnClickModal: false,
          closeOnPressEscape: false,
        },
      );

      // 用户点击确定后，跳转到主系统重新登录
      await authStore.logout();
    } catch {
      // 用户取消或其他错误，仍然跳转到主系统
      await authStore.logout();
    }
  }

  return {
    showLoginStatusErrorDialog,
  };
}
